#!/bin/bash

echo "========================================"
echo "启动项目管理系统后端服务"
echo "========================================"
echo

# 获取脚本所在目录的父目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"

cd "$PROJECT_DIR"

echo "检查Java环境..."
if ! command -v java &> /dev/null; then
    echo "错误：未找到Java环境，请确保已安装JDK 8或更高版本"
    exit 1
fi

echo "检查Maven环境..."
if ! command -v mvn &> /dev/null; then
    echo "错误：未找到Maven环境，请确保已安装Maven 3.6或更高版本"
    exit 1
fi

echo "进入后端目录..."
cd backend

echo "检查项目是否已编译..."
if [ ! -f "target/project-management-0.0.1-SNAPSHOT.jar" ]; then
    echo "项目未编译，开始编译..."
    mvn clean package -DskipTests
    if [ $? -ne 0 ]; then
        echo "编译失败，请检查错误信息"
        exit 1
    fi
else
    echo "发现已编译的jar文件，跳过编译步骤"
fi

echo
echo "启动后端服务..."
echo "服务地址：http://localhost:8080"
echo "按 Ctrl+C 停止服务"
echo

java -jar target/project-management-0.0.1-SNAPSHOT.jar

echo
echo "后端服务已停止"